/**
 * Created by ROGSTAR on 2016/11/29.
 */
require.config(window.jshead);
require(['jquery', 'pages', "layer",'show','navigator','radialIndicator'], function ($, pg,layer,sw) {
    layer.config({
        path: '/js/layer/'
    });
    var itemType='';
    //设置一个最小高度
    var h=$(window).height()-456+'px';
    $('.list_container').css('min-height',h);
    var getItemCycle=decodeURI(window.location.href).split('=')[1];
    var url="/item/getPcItemList";
    var params={
        'itemCycle':0,
        'itemType': 0,
        'itemRepayMethod': 0,
        'itemRate':0};
    if(getItemCycle){
        $(".nav-middelUl li").each(function(){
           if($(this).data().itemcycle==getItemCycle){
               $(this).addClass('active-middleUlli');
               $(this).siblings().removeClass('active-middleUlli');
           }
        });
        params.itemCycle=getItemCycle;
    }
    //可投项目列表
    showList(params,url);
    function showList(param,url){
        pg.paging(1, 11,url , 'pcItemList', 'pcItemListTemp',param , '', validItemCallback);
    }
    $(".nav-middelUl li").bind("click",function(){
        for(x in $(this).data()){
            var key=x;
        }
        for(i in params){
            if(i.toLowerCase()==key.toLowerCase()){
                params[i]=$(this).data(i.toLowerCase());
            }
        }
        showList(params,url);
    });
    //期限切换
    $('#validItem .tab').click(function () {
        $(this).addClass('list_active');
        $('#validItem .tab').not($(this)).removeClass('list_active');

        if(itemType==''){
            showList({itemCycle:$(this).index()});
        }else{
            showList({
                    itemCycle:$(this).index(),
                    itemType:itemType
                }
            );
        }

    });

    //项目排序
    function orderFunc(ele){
        var desc=false;
        ele.click(function () {
            $(this).addClass('table_header_active');
            $('.order_item').not($(this)).removeClass('table_header_active');
            desc=!desc;
            if(!desc) {
                $(this).find('.order').removeClass('order_top').addClass('order_down');
                showList({
                    itemCycle: 0,
                    orderName: $(this).attr('data-name'),
                    desc: 'desc'
                })

            } else{
                $(this).find('.order').removeClass('order_down').addClass('order_top');
                showList({
                    itemCycle:0,
                    orderName:$(this).attr('data-name'),
                    desc:''
                })

            }
        });
    }
//  orderFunc($('#itemRate'));
//  orderFunc($('#itemCycle'));

    //产品分类
    $('#itemType').change(function () {
        itemType=$(this).val();
        var _thisIndex=0;
        $('#validItem .tab').each(function () {
            if($(this).hasClass('list_active')){
                _thisIndex=$(this).index();
            }
        });

        if($(this).val()==''){
            showList({
                itemCycle:_thisIndex
            })
        }else{
            showList({
                itemCycle:_thisIndex,
                itemType:itemType
            })
        }


    });


    //查看历史项目
    $('#historyItem').click(function () {
        $('.order_item').removeClass('table_header_active');
        $('.order').removeClass('order_down order_top');

        if($(this).text()=='历史项目'){
            $(this).addClass('list_active').html('<strong style="font-size: 20px;">历史项目/</strong><span style="font-size:16px;">可投项目</span>');
            $('.ketou').hide();
            $('.lishi').show();
            pg.paging(1, 11, "/item/getPcHistoryItemList", 'pcItemList', 'pcItemListTemp','' , '', historyItemCallback);
        }else{
            $('#itemType').val('');
            $('#historyItem').removeClass('list_active').text('历史项目');
            $('.ketou').show();
            $('.lishi').hide();
            $('#validItem .tab').removeClass('list_active');
            $('#quanbu').addClass('list_active');
            showList({itemCycle:0});
        }
    });



    function validItemCallback(){
        radialIndicator.defaults.radius = 34;
        radialIndicator.defaults.barColor = "#fcb22f";
        radialIndicator.defaults.barBgColor = "#e8e8e8";
        radialIndicator.defaults.barWidth = 5;
        radialIndicator.defaults.percentage = true;
        radialIndicator.defaults.displayNumber = true;
        radialIndicator.defaults.fontSize = 20;
        radialIndicator.defaults.fontWeight = 'normal';
        $('.itemScale').each(function () {
            $(this).radialIndicator();
            $(this).data('radialIndicator').value($(this).attr('data-value'));
        });
        $('.countdown').each(function () {
            var obj=$(this);
            var countdownTime=$(this).attr('data-time');
            timer(countdownTime/1000,obj);
        });

    }

    function historyItemCallback(){
        radialIndicator.defaults.radius = 34;
        radialIndicator.defaults.barColor = "#e8e8e8";
        radialIndicator.defaults.barBgColor = "#e8e8e8";
        radialIndicator.defaults.barWidth = 5;
        radialIndicator.defaults.percentage = true;
        radialIndicator.defaults.displayNumber = true;
        radialIndicator.defaults.fontSize = 20;
        radialIndicator.defaults.fontWeight = 'normal';
        $('.itemScale').each(function () {
            $(this).radialIndicator();
            $(this).data('radialIndicator').value($(this).attr('data-value'));
        });
    }


    function timer(intDiff,obj){
        if( obj.timers){
            clearInterval( obj.timers);
        }

        obj.timers=setInterval(function(){
            var day=0,
                hour=0,
                minute=0,
                second=0;//时间默认值
            if(intDiff > 0){
                day = Math.floor(intDiff / (60 * 60 * 24));
                hour = Math.floor(intDiff / (60 * 60)) - (day * 24);
                minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60);
                second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
            }

            if (minute <= 9) minute = '0' + minute;
            if (second <= 9) second = '0' + second;
            obj.find('.hour').html(hour);
            obj.find('.min').html(minute);
            obj.find('.sec').html(second);
            intDiff--;
        }, 1000);
    }



    /*点击切换散标专区和债转专区点击切换与显示*/
    $('.navbar-item').bind('click',function(){
        $('.nav-middleUlli').removeClass('active-middleUlli');
        $(".a ul li:nth-child(1)")
        $(".nav-middelUl li:nth-child(1)").addClass('active-middleUlli');
        $(this).addClass('active-navbarItem');
        $(this).siblings().removeClass('active-navbarItem');
        if($(this).html()=='债转专区'){
            $('.debt').show();
            $('.standard_powder').hide();
            url="/debt/investList";
            params={
                'days':0,
                'rate': 0,
                'amount': 0,
                'itemRepayMethod':0};
        }else{
            $('.debt').hide();
            $('.standard_powder').show();
            url="/item/getPcItemList";
            params={
                'itemCycle':0,
                'itemType': 0,
                'itemRepayMethod': 0,
                'itemRate':0};
        }
        showList(params,url);
    })

    /*选项列表点击动画效果*/
    $('.nav-middleUlli').bind('click',function(){
        $(this).addClass('active-middleUlli');
        $(this).siblings().removeClass('active-middleUlli');
    })

    //常见问题列表
    $.ajax({
        type:'post',
        url:'/problem/problemList',
        data:{recommend:1},
        dataType:'json',
        success:function(data){
            var item = ''
            if(data.list.length>8){
                data.list.length=9
            }else{
                data.list.length==data.list.length
            }
            for (var i=0;i<data.list.length;i++){
                var a = data.list[i].answer.replace(/(\n)/g, "").replace(/(\t)/g, "").replace(/(\r)/g, "").replace(/<\/?[^>]*>/g, "").replace(/\s*/g, "");
                if(a.length>13){
                     a = a.substring(0,12)+'....?'
                }else{
                    a
                }
                var item =item+'<li><a href="/problem?0?5?'+data.list[i].type+'?'+data.list[i].id+'" class="problemItem">'+a+"</a></li>"
                $('.table-right-ul').html(item)
            }

        }
    })
});